package com.hzvdvmc.autosale.sale.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.hzvdvmc.autosale.R;
import com.hzvdvmc.autosale.common.OnItemClickListener;
import com.hzvdvmc.autosale.sale.bean.Good;
import com.hzvdvmc.autosale.sale.bean.ShopListRespnse;

import java.util.List;

/**
 * Created by william on 2017/11/10.
 * 首页商品列表
 */

public class GoodsAdapter extends RecyclerView.Adapter<GoodsAdapter.GoodsViewHodler> {
    private List<ShopListRespnse.RetObjBean> goodsList;
    private Context mContext;
    private OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public GoodsAdapter(List<ShopListRespnse.RetObjBean> goodsList) {
        this.goodsList = goodsList;
    }

    @Override
    public GoodsViewHodler onCreateViewHolder(ViewGroup parent, int viewType) {
        mContext = parent.getContext();
        View rootView = LayoutInflater.from(mContext).inflate(R.layout.item_good_list, parent, false);
        return new GoodsViewHodler(rootView);
    }

    @Override
    public void onBindViewHolder(GoodsViewHodler holder, final int position) {
        if (onItemClickListener != null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onItemClickListener.onClick(v, position);
                }
            });
        }

        if (goodsList.get(position).getCurrStock() != null
                && goodsList.get(position).getCurrStock().equals("0")) {
            holder.ivItem.setAlpha(0.4f);
            holder.ivEmptyFlag.setVisibility(View.VISIBLE);
        } else {
            holder.ivItem.setAlpha(1f);
            holder.ivEmptyFlag.setVisibility(View.GONE);
        }

        holder.tvInfo.setText("￥: " + goodsList.get(position).getAPrice());
        Glide.with(mContext).load(goodsList.get(position).getPicUrl()).into(holder.ivItem);
    }

    @Override
    public int getItemCount() {
        return goodsList == null ? 0 : goodsList.size();
    }

    class GoodsViewHodler extends RecyclerView.ViewHolder {
        ImageView ivItem, ivEmptyFlag;
        TextView tvInfo;

        public GoodsViewHodler(View itemView) {
            super(itemView);
            ivItem = (ImageView) itemView.findViewById(R.id.iv_item);
            tvInfo = (TextView) itemView.findViewById(R.id.tv_info);
            ivEmptyFlag = (ImageView) itemView.findViewById(R.id.iv_emptyflag);
        }
    }

}
